/*
 * @lc app=leetcode.cn id=141 lang=cpp
 *
 * [141] 环形链表
 *
 * 方法1：快慢指针
 * - 细节耗时
 *
 * 官方题解：优化快慢指针的判断
 * - 默认存在环
 * - 块指针的处理
 */

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(nullptr) {}
};

// @lc code=start
class Solution
{
public:
    bool hasCycle_01(ListNode *head)
    {
        ListNode *slow = head;
        if (slow == nullptr)
            return false;

        ListNode *fast = slow->next;

        while (fast != nullptr)
        {
            if (fast == slow)
                return true;

            slow = slow->next;
            fast = fast->next;
            if (fast != nullptr)
                fast = fast->next;
        }

        return false;
    }

    bool hasCycle_00(ListNode *head)
    {
        if (head == nullptr || head->next == nullptr)
            return false;

        ListNode *slow = head;
        ListNode *fast = head->next;

        while (slow != fast)
        {
            if (fast == nullptr || fast->next == nullptr)
                return false;

            slow = slow->next;
            fast = fast->next->next;
        }

        return true;
    }
};
// @lc code=end
